package mystia.lorelei.app.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import mystia.lorelei.app.entity.Activity;
import mystia.lorelei.app.entity.Member;
import mystia.lorelei.app.entity.Purchase;
import mystia.lorelei.app.entity.dto.PurchaseDTO;
import mystia.lorelei.app.entity.dto.PurchaseItem;
import mystia.lorelei.app.service.ActivityService;
import mystia.lorelei.app.service.CuisineService;
import mystia.lorelei.app.service.MemberService;
import mystia.lorelei.app.service.PurchaseService;
import mystia.lorelei.app.util.Result;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/purchase")
@Tag(name = "订单管理")
public class PurchaseController {
	@Resource
	private PurchaseService purchaseService;
	@Resource
	private CuisineService cuisineService;
	@Resource
	private MemberService memberService;
	@Resource
	private ActivityService activityService;
	
	@PostMapping("/save")
	@Operation(summary = "保存订单",description = "")
	public Result savePurchase(@RequestBody PurchaseDTO purchaseDTO) {
		try {
			Member member = memberService.queryMemberById(purchaseDTO.getMemberId());
			BigDecimal total = new BigDecimal(0);
			Integer getIntegration = 0;
			for (PurchaseItem item : purchaseDTO.getPurchaseItems()) {
				BigDecimal price = cuisineService.queryCuisine(item.getCuisineId()).price;  //单价
				price = price.multiply(BigDecimal.valueOf(item.getNumber()));               //数量
				
				// 判断会员是否满足获取条件：等级足够、积分足够
				Activity activity = activityService.queryActivityCorrespond(item.getCuisineId(), member.getGrade());
				if (activity != null) {
					getIntegration += (int) (0.6 * price.doubleValue()); //消费获取的积分
					if(activity.getDepleteIntegration()<=member.getIntegration()){
						price = price.multiply(BigDecimal.valueOf(activity.getDiscount()));     //折扣
						member.setIntegration(member.getIntegration() - activity.getDepleteIntegration());
					}
				}
				total = total.add(price);
			}
			member.setIntegration(member.getIntegration()+getIntegration);
			purchaseDTO.setTotalPrice(total);
			purchaseService.savePurchase(new Purchase(purchaseDTO));
			memberService.saveMember(member);
			return Result.ok();
		} catch (Exception e) {
			return Result.fail();
		}
	}
	
	@DeleteMapping("/delete/{id}")
	@Operation(summary = "删除订单")
	public Result deletePurchase(@PathVariable("id") int id) {
		try {
			purchaseService.deletePurchase(id);
			return Result.ok();
		} catch (Exception exception) {
			return Result.fail();
		}
	}
	
	@DeleteMapping("/delete")
	@Operation(summary = "批量删除活动")
	public Result deletePurchases(@RequestBody List<Integer> ids){
		try {
			purchaseService.deletePurchases(ids);
			return Result.ok();
		}catch (Exception e){
			return Result.fail();
		}
	}
	
	@GetMapping("/list")
	@Operation(summary = "批量获取", description = "获取所有的订单,可用于导出订单表")
	public Result<List<Purchase>> listActivity() {
		return Result.ok(purchaseService.listPurchase());
	}
}
